package cn.tedu.eguser.controller;

import cn.tedu.eguser.mapper.UserMapper;
import cn.tedu.eguser.pojo.dto.UserAddParam;
import cn.tedu.eguser.pojo.dto.UserUpdateParam;
import cn.tedu.eguser.pojo.entity.User;
import cn.tedu.eguser.pojo.vo.UserListVO;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Validated
@Slf4j
@EnableKnife4j
@RestController
@RequestMapping("/v1/user/")
public class UserController {

    @Autowired
    UserMapper userMapper;


    //添加用户
    @PostMapping("add")
    public String addUser(@Validated  @RequestBody UserAddParam userAddParam) {
        User user = new User();

        BeanUtils.copyProperties(userAddParam, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());

        return userMapper.insertUser(user) > 0 ? "添加用户成功" : "添加用户失败";
    }


    //查询用户列表
    @GetMapping("list")
    public List<UserListVO> selectUserList() {
        System.out.println("查询所有用户列表:");
        return userMapper.selectUserList();
    }

    //删除指定用户
    @PostMapping("del")
    public String deleteUserById(Long id) {
        int count = userMapper.deleteUserById(id);
        return count > 0 ? "删除用户成功" : "删除用户失败";
    }

    //修改用户信息
    @PostMapping("update")
    public String updateUserById(@RequestBody UserUpdateParam userUpdateParam) {
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam, user);
        user.setUpdateTime(new Date());
        int count = userMapper.updateUserById(user);
        return count > 0 ? "修改用户成功" : "修改用户失败";
    }


}
